package com.hsd.znsh.converter;

import com.hsd.znsh.entity.Sensor;
import com.hsd.znsh.enums.OnlineStatusEnum;
import com.hsd.znsh.pojo.output.MonitorVO;
import com.hsd.znsh.pojo.output.SensorVO;
import com.hsd.znsh.repository.HostRepository;
import com.hsd.znsh.repository.SensorTypeRepository;
import com.hsd.znsh.repository.UserRepository;
import com.hsd.znsh.utils.BeanUtil;
import com.hsd.znsh.utils.EnumUtil;

import java.util.ArrayList;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-25 13:54:52
 */
public class SensorConverter {

    public static SensorVO converterToSensorVO(Sensor sensor, HostRepository hostRepository,
                                               UserRepository userRepository,
                                               SensorTypeRepository sensorTypeRepository){
        if(sensor==null) return null;
        SensorVO sensorVO=new SensorVO();
        BeanUtil.copyProperties(sensor,sensorVO);
        if(sensor.getHostId()!=null){
            sensorVO.setHostName(hostRepository.findOne(sensor.getHostId()).getHostName());
        }
        if(sensor.getCreateUserId()!=null){
            sensorVO.setCreateUsername(userRepository.findOne(sensor.getCreateUserId()).getUsername());
        }
        if(sensor.getSensorTypeId()!=null){
            sensorVO.setSensorType(sensorTypeRepository.getOne(sensor.getSensorTypeId()).getName());
        }
        sensorVO.setOnlineStatusString(EnumUtil.getMessageByCode(sensor.getOnlineStatus(),OnlineStatusEnum.class));
        return sensorVO;
    }

    public static List<SensorVO> converterToSensorVOList(List<Sensor> sensors, HostRepository hostRepository,
                                                         UserRepository userRepository,
                                                         SensorTypeRepository sensorTypeRepository){
        List<SensorVO> list=new ArrayList<>();
        for(Sensor sensor:sensors){
            list.add(converterToSensorVO(sensor,hostRepository,userRepository,sensorTypeRepository));
        }
        return list;
    }
}
